package com.xinke.config;


import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:application.properties")
public class DataSourceConfig {

    // 源数据库配置
    @Value("${source.datasource.url}")
    private String sourceUrl;
    @Value("${source.datasource.username}")
    private String sourceUsername;
    @Value("${source.datasource.password}")
    private String sourcePassword;
    @Value("${source.datasource.driver-class-name}")
    private String sourceDriver;

    // 目标数据库配置
    @Value("${target.datasource.url}")
    private String targetUrl;
    @Value("${target.datasource.username}")
    private String targetUsername;
    @Value("${target.datasource.password}")
    private String targetPassword;
    @Value("${target.datasource.driver-class-name}")
    private String targetDriver;

    // 源数据库数据源
    @Bean(name = "sourceDataSource")
    public DataSource sourceDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(sourceUrl);
        config.setUsername(sourceUsername);
        config.setPassword(sourcePassword);
        config.setDriverClassName(sourceDriver);
        return new HikariDataSource(config);
    }

    // 目标数据库数据源（@Primary标识默认数据源，可选）
    @Primary
    @Bean(name = "targetDataSource")
    public DataSource targetDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(targetUrl);
        config.setUsername(targetUsername);
        config.setPassword(targetPassword);
        config.setDriverClassName(targetDriver);
        return new HikariDataSource(config);
    }
}